<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        img {
            vertical-align: top;
            /* display: block; */
        }

        .judgeWrapper {
            width: 600px;
            /* height: 300px; */
            /* border: 1px solid #333; */
            margin: 50px auto;
        }

        .judgeWrapper .judgeBox {
            width: 100%;
            position: relative;
        }

        .judgeWrapper .judgeBox img {
            width: 100%;
            height: 338px;
        }

        .judgeWrapper .judgeBox .boxA,
        .judgeWrapper .judgeBox .boxB {
            width: 50px;
            height: 50px;
            background-color: #fff;
            position: absolute;
        }

        .judgeWrapper .judgeBox .boxA {
            background: url(../images/bg.png) no-repeat -300px -150px / 600px auto;
            left: 0;
            top: 150px;
            z-index: 1;
        }

        .judgeWrapper .judgeBox .boxB {
            left: 300px;
            top: 150px;
        }

        .judgeWrapper .sliderBar {
            width: 100%;
            height: 50px;
            background-color: grey;
            border-radius: 25px;
            margin-top: 10px;
            position: relative;
        }

        .judgeWrapper .sliderBar .ball {
            width: 50px;
            height: 50px;
            background-color: pink;
            position: absolute;
            left: 0;
            top: 0;
            border-radius: 50%;
        }
    </style>
</head>

<body>
    <div class="judgeWrapper">
        <div class="judgeBox">
            <img src="../images/bg.png" alt="">
            <div class="boxA"></div>
            <div class="boxB"></div>
        </div>
        <div class="sliderBar">
            <div class="ball"></div>
        </div>
    </div>
</body>
<script>
    // var img = document.querySelector("img");
    // img.addEventListener("load",function(){
    // })

    // 问题: boxB随机位置范围问题:
    //  judgeBox 依赖图片撑开,图片加载需要时间的,在图片没有加载完成之前, judgeBox高度有问题
    // 解决方法:
    // 1. 给图片设置定高 => 无论图片是否加载完毕,都是固定高度
    // 2. 等图片加载完毕之后,在绑定相关的事件

    var judgeWrapper = document.querySelector(".judgeWrapper");
    var judgeBox = document.querySelector(".judgeBox");
    var slideBar = document.querySelector(".sliderBar");
    var ball = document.querySelector(".ball");
    var boxA = document.querySelector(".boxA");
    var boxB = document.querySelector(".boxB");

    var maxLeft = slideBar.clientWidth - ball.clientWidth;
    var maxTop = judgeBox.clientHeight - boxB.clientHeight;


    randPos();

    // // 200-maxLeft
    // var randX = 200 + Math.round(Math.random() * (maxLeft - 200));
    // var randY = Math.round(Math.random() * maxTop);

    // boxA.style.top = randY + "px";
    // // boxA.style.backgroundPosition = "-300px -150px";
    // // boxA.style.backgroundPosition = `${-randX}px ${-randY}px`;
    // boxA.style.backgroundPositionX = (-randX) + "px";
    // boxA.style.backgroundPositionY = (-randY) + "px";

    // boxB.style.left = randX + "px";
    // boxB.style.top = randY + "px";

    ball.onmousedown = function (e) {
        var e = e || window.event;

        var posX = e.offsetX;

        document.onmousemove = function (e) {
            var e = e || window.event;

            var x = e.pageX - slideBar.offsetLeft - posX;

            if (x < 0) x = 0;
            if (x > maxLeft) x = maxLeft;

            ball.style.left = x + "px";
            boxA.style.left = x + "px";

        }

        document.onmouseup = function (e) {
            document.onmousemove = null;

            // 松开时进行验证
            // 理想情况下: boxA和boxB重叠  => 两个盒子的left值相同 => boxA.offsetLeft ==boxB.offsetLeft  => 对手残党不友好 

            // 解决方法: 设置无法允许范围  [boxB.offsetLeft-2,boxB.offsetLeft+2]

            if(boxA.offsetLeft){ // 如果boxA移动了在进行验证
                if (Math.abs(boxA.offsetLeft - boxB.offsetLeft) <= 2) {
                    console.log("解锁成功");
                    location.href = "https://www.baidu.com";
                } else {
                    ball.style.left = 0;
                    boxA.style.left = 0;

                    randPos();
                }
            }


        }

    }
    


    function randPos(){
        // 200-maxLeft
        var randX = 200 + Math.round(Math.random() * (maxLeft - 200));
        var randY = Math.round(Math.random() * maxTop);

        boxA.style.top = randY + "px";
        // boxA.style.backgroundPosition = "-300px -150px";
        // boxA.style.backgroundPosition = `${-randX}px ${-randY}px`;
        boxA.style.backgroundPositionX = (-randX) + "px";
        boxA.style.backgroundPositionY = (-randY) + "px";

        boxB.style.left = randX + "px";
        boxB.style.top = randY + "px";

    }
</script>

</html>